package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.dto.DevicePropertyDto;
import com.zzyl.nursing.dto.MemberDeviceDataDto;
import com.zzyl.nursing.dto.UserDto;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.service.IAlertDataService;
import com.zzyl.nursing.service.IFamilyMemberElderService;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.vo.member.LoginVo;
import com.zzyl.nursing.vo.member.MemberVo;
import com.zzyl.nursing.vo.member.PropertyStatusInfoVoList;
import com.zzyl.nursing.vo.member.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zzw
 * @date 2025/6/3
 * @description 老人家属端接口
 */
@RestController
@RequestMapping("/member/user")
@Api(tags = "老人家属端接口")
public class FamilyMemberController extends BaseController {

    @Autowired
    private IFamilyMemberService familyMemberService;

    @Autowired
    private IFamilyMemberElderService familyMemberElderService;

    @Autowired
    private IAlertDataService alertDataService;

    /**
     * 小程序登录
     */
    @PostMapping("/login")
    @ApiOperation("小程序登录")
    public AjaxResult login(@RequestBody UserLoginRequestDto userLoginRequestDto) {
        LoginVo loginVo = familyMemberService.login(userLoginRequestDto);
        return super.success(loginVo);
    }

    /**
     * 绑定家人
     */
    @PostMapping("/add")
    @ApiOperation("绑定家人")
    public AjaxResult add(@RequestBody UserDto userDto) {
        familyMemberService.add(userDto);
        return success();
    }

    /**
     * 查询家人列表
     *
     * @return List<UserVo>
     */
    @GetMapping("/my")
    public R<List<UserVo>> my() {
        return R.ok(familyMemberService.my());
    }

    /**
     * 查看已绑定家人列表
     *
     * @return TableDataInfo<MemberVo>
     */
    @GetMapping("/list-by-page")
    public R<List<MemberVo>> listByPage() {
        startPage();
        List<MemberVo> memberVoList = familyMemberService.listByPage2();
        return R.ok(memberVoList);
    }

    /**
     * 查询家人健康数据
     *
     * @return List<PropertyStatusInfo>
     */
    @PostMapping("/QueryDevicePropertyStatus")
    public R<PropertyStatusInfoVoList> queryDevicePropertyStatus(@RequestBody DevicePropertyDto devicePropertyDto) {
        PropertyStatusInfoVoList propertyStatusInfoVoList = familyMemberService.queryDevicePropertyStatus(
                devicePropertyDto);
        return R.ok(propertyStatusInfoVoList);
    }

    /**
     * 解绑家人
     *
     * @param elderId 老人id
     */
    @DeleteMapping("/deleteById")
    public AjaxResult deleteById(Long elderId) {
        familyMemberElderService.handleUnbind(elderId);
        return success();
    }

    /**
     * 按天统计指标数据
     *
     * @param memberDeviceDataDto 查询参数
     * @return List<Map < String, Double>>
     */
    @GetMapping("/queryDeviceDataListByDay")
    public R<List<Map<String, Double>>> queryDeviceDataListByDay(MemberDeviceDataDto memberDeviceDataDto) {
        List<Map<String, Double>> list = familyMemberService.queryDeviceDataListByDay(memberDeviceDataDto);
        return R.ok(list);
    }

    /**
     * 按周统计指标数据
     *
     * @param memberDeviceDataDto 统计参数
     * @return TableDataInfo<Map < String, Double>>
     */
    @GetMapping("/queryDeviceDataListByWeek")
    public R<List<Map<String, Double>>> queryDeviceDataListByWeek(MemberDeviceDataDto memberDeviceDataDto) {
        List<Map<String, Double>> list = familyMemberService.queryDeviceDataListByWeek(memberDeviceDataDto);
        return R.ok(list);
    }

    /**
     * 分页查询报警数据
     *
     * @param memberDeviceDataDto 查询条件
     * @return 分页数据
     */
    // TODO 后续完成
    @GetMapping("/pageQueryAlertData")
    @ApiOperation("分页查询报警数据")
    public TableDataInfo<AlertData> pageQueryAlertData(MemberDeviceDataDto memberDeviceDataDto) {
        startPage();
        return getDataTable(null);
    }
}
